package com.jxpanda.infrastructure.crypto.algorithm.encrypt;

import com.jxpanda.infrastructure.crypto.algorithm.encrypt.contract.AbstractSymmetricEncrypt;
import com.jxpanda.infrastructure.crypto.constant.AlgorithmEncrypt;
import com.jxpanda.infrastructure.crypto.toolkit.IvKit;
import lombok.Getter;

import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

@Getter
public class DesEncrypt extends AbstractSymmetricEncrypt {

    public DesEncrypt(AlgorithmEncrypt.DES des, SecretKey key) {
        super(des.getAlgorithm(), key);
    }

    public DesEncrypt(AlgorithmEncrypt.DES des, SecretKey key, byte[] ivBytes) {
        super(des.getAlgorithm(), key, ivBytes);
    }

    public DesEncrypt(AlgorithmEncrypt.DES des, SecretKey key, IvParameterSpec ivParameterSpec) {
        super(des.getAlgorithm(), key, ivParameterSpec);
    }

    public static DesEncrypt createDefault() {
        return AlgorithmEncrypt.DES.DES_CBC_PKCS5.builder().buildDefault();
    }

    public static DesEncrypt.Builder builder(AlgorithmEncrypt.DES des) {
        return new DesEncrypt.Builder(des);
    }

    public static class Builder extends AbstractSymmetricEncrypt.Builder<AlgorithmEncrypt.DES, DesEncrypt> {
        protected Builder(AlgorithmEncrypt.DES des) {
            super(des);
        }

        @Override
        public DesEncrypt buildDefault() {
            SecretKey key = getKeyOrDefault();
            IvParameterSpec ivParameterSpec = getIvParameterSpecOrDefault(IvKit.generateDesIv());
            return new DesEncrypt(getSymmetricAlgorithm(), key, ivParameterSpec);
        }
    }
}
